package reggie.ruiji.service.impl;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reggie.ruiji.except.Customexception;
import reggie.ruiji.mapper.CategoryMapper;
import reggie.ruiji.pojo.Category;
import reggie.ruiji.pojo.Employee;
import reggie.ruiji.pojo.PageBean;
import reggie.ruiji.service.CategoryService;
import reggie.ruiji.service.DishService;
import reggie.ruiji.service.SetmealService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categorymapper;
    @Autowired
    private DishService   dishService;
    @Autowired
    private SetmealService  setmealService;

    @Override
    public void save(Category category, HttpServletRequest request) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser((Long) request.getSession().getAttribute("employee"));
        category.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        categorymapper.save(category);
        return ;
    }

    @Override
    public PageBean pagee(Integer pageNo, Integer pageSize) {
        List<Category> emplist= categorymapper.list();
        PageBean pageBean = new PageBean(emplist,emplist.size(),pageSize,pageNo);
        return pageBean;
    }

    @Override
    public void delete(Long id) {
        Long one=setmealService.count(id);
        Long two=dishService.count(id);
        if(one>0||two>0){
            throw new Customexception("该分类下有菜品或套餐，不能删除");
        }
        categorymapper.delete(id);
    }

    @Override
    public void update(Category category, HttpServletRequest request) {
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        categorymapper.updatebyid(category);
    }

    @Override
    public List<Category> list(Integer type) {
        return  categorymapper.listByType(type);
    }
}
